/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardumen2d;

import java.awt.Dimension;

/**
 *
 * @author jorgeorm
 */
public class Cardumen2D extends javax.swing.JFrame {
    /**
     * Creates new form Cardumen2D
     */
    public Cardumen2D() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jp_form = new javax.swing.JPanel();
        jp_config = new javax.swing.JPanel();
        jl_numPeces = new javax.swing.JLabel();
        jsl_numPeces = new javax.swing.JSlider();
        jb_comenzar = new javax.swing.JButton();
        jb_detener = new javax.swing.JButton();
        jp_lienzo = new javax.swing.JPanel();
        lienzoPecera = null;

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cardumen");
        setResizable(false);

        jp_config.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Configuración", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 0, 12), new java.awt.Color(51, 51, 51))); // NOI18N

        jl_numPeces.setText("Numero Peces:");

        jsl_numPeces.setFont(new java.awt.Font("Dialog", 1, 10)); // NOI18N
        jsl_numPeces.setMajorTickSpacing(500);
        jsl_numPeces.setMaximum(1010);
        jsl_numPeces.setMinimum(10);
        jsl_numPeces.setMinorTickSpacing(250);
        jsl_numPeces.setPaintLabels(true);
        jsl_numPeces.setPaintTicks(true);

        javax.swing.GroupLayout jp_configLayout = new javax.swing.GroupLayout(jp_config);
        jp_config.setLayout(jp_configLayout);
        jp_configLayout.setHorizontalGroup(
            jp_configLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jsl_numPeces, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addComponent(jl_numPeces, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
        );
        jp_configLayout.setVerticalGroup(
            jp_configLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_configLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jl_numPeces)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jsl_numPeces, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(188, Short.MAX_VALUE))
        );

        jb_comenzar.setText("Comenzar");
        jb_comenzar.setSelected(true);
        jb_comenzar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_comenzarActionPerformed(evt);
            }
        });

        jb_detener.setText("Detener");
        jb_detener.setEnabled(false);
        jb_detener.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_detenerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jp_formLayout = new javax.swing.GroupLayout(jp_form);
        jp_form.setLayout(jp_formLayout);
        jp_formLayout.setHorizontalGroup(
            jp_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_formLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jp_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jp_config, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jb_comenzar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jb_detener, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(3, 3, 3))
        );
        jp_formLayout.setVerticalGroup(
            jp_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_formLayout.createSequentialGroup()
                .addComponent(jp_config, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_comenzar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_detener)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jp_lienzo.setPreferredSize(new java.awt.Dimension(581, 576));
        jp_lienzo.setLayout(null);
        /*Toco añadirlo así para solucionar problema con el
        canvas*/
        int width, height;
        width = (int) jp_lienzo.getPreferredSize().getWidth();
        height = (int) jp_lienzo.getPreferredSize().getHeight();

        pecera = new CanvasPecera(width, height);

        lienzoPecera = pecera;
        jp_lienzo.add(lienzoPecera);
        lienzoPecera.setBounds(0, 0, 581, 576);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jp_form, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jp_lienzo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jp_lienzo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jp_form, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_comenzarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_comenzarActionPerformed
        // TODO add your handling code here:
        jb_comenzar.setEnabled(false);
        jb_detener.setEnabled(true);
        pecera.iniciarCardumen(jsl_numPeces.getValue());
        
        //Hago que corra en un hilo
        hiloPecera = new Thread(pecera);
        hiloPecera.start();
    }//GEN-LAST:event_jb_comenzarActionPerformed

    private void jb_detenerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_detenerActionPerformed
        // TODO add your handling code here:
        jb_comenzar.setEnabled(true);
        jb_detener.setEnabled(false);
        pecera.detener();
    }//GEN-LAST:event_jb_detenerActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Cardumen2D.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Cardumen2D.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Cardumen2D.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Cardumen2D.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Cardumen2D().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jb_comenzar;
    private javax.swing.JButton jb_detener;
    private javax.swing.JLabel jl_numPeces;
    private javax.swing.JPanel jp_config;
    private javax.swing.JPanel jp_form;
    private CanvasPecera pecera;
    private javax.swing.JPanel jp_lienzo;
    private javax.swing.JSlider jsl_numPeces;
    private java.awt.Canvas lienzoPecera;
    // End of variables declaration//GEN-END:variables
    private Thread hiloPecera;
}
